#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>

#define PAGE_SIZE 4096

int main(int argc , char *argv[]) {
    int fd;
    int i;
    char *p_map;
    
    //打开设备
    fd = open("mmap.txt", O_RDWR);
    if(fd < 0) {
        printf("open fail\n");
        exit(1);
    }

    //内存映射
    p_map = (char *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(p_map == MAP_FAILED) {
        printf("mmap fail\n");
        munmap(p_map, PAGE_SIZE);
        exit(1);
    }

    //打印映射后的内存中的前10个字节内容
    for(i=0; i<10; i++)
        printf("%c\n", p_map[i]);
    
    return 0;

}